package org.abc.fund.controller.dataUpdate;
import lombok.extern.slf4j.Slf4j;
import org.abc.fund.service.dataSourceCollection.FundHoldingBatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/holding/update")
@Slf4j
public class HoldingUpdateController {
    @Autowired
    private FundHoldingBatchService holdingBatchService;

    //手动触发指定年份持仓更新
    @PostMapping("/trigger/year/{year}")
    public ResponseEntity<Map<String, Object>> triggerYearUpdate(@PathVariable int year) {
        log.info("手动触发 {} 年持仓更新", year);
        Map<String, Object> result = new HashMap<>();
        try {
            holdingBatchService.fetchAllFundHoldings(year);
            result.put("success", true);
            result.put("message", year + "年持仓更新完成");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("手动触发 {} 年持仓更新失败", year, e);
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    //手动触发季度持仓更新
    @PostMapping("/trigger/quarterly")
    public ResponseEntity<Map<String, Object>> triggerQuarterlyUpdate() {
        log.info("手动触发季度持仓更新");
        Map<String, Object> result = new HashMap<>();
        try {
            holdingBatchService.fetchQuarterlyHoldings();
            result.put("success", true);
            result.put("message", "季度持仓更新完成");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("手动触发季度持仓更新失败", e);
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    //手动触发半年度持仓更新
    @PostMapping("/trigger/semi-annual")
    public ResponseEntity<Map<String, Object>> triggerSemiAnnualUpdate() {
        log.info("手动触发半年度持仓更新");
        
        Map<String, Object> result = new HashMap<>();
        try {
            holdingBatchService.fetchSemiAnnualHoldings();
            result.put("success", true);
            result.put("message", "半年度持仓更新完成");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("手动触发半年度持仓更新失败", e);
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    //手动触发年度持仓更新
    @PostMapping("/trigger/annual")
    public ResponseEntity<Map<String, Object>> triggerAnnualUpdate() {
        log.info("手动触发年度持仓更新");
        
        Map<String, Object> result = new HashMap<>();
        try {
            holdingBatchService.fetchAnnualHoldings();
            result.put("success", true);
            result.put("message", "年度持仓更新完成");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("手动触发年度持仓更新失败", e);
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    //手动触发数据清理
    @PostMapping("/trigger/cleanup")
    public ResponseEntity<Map<String, Object>> triggerCleanup() {
        log.info("手动触发持仓数据清理");
        
        Map<String, Object> result = new HashMap<>();
        try {
            holdingBatchService.cleanupOldHoldings();
            result.put("success", true);
            result.put("message", "持仓数据清理完成");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("手动触发持仓数据清理失败", e);
            result.put("success", false);
            result.put("message", "清理失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    //获取支持的年份列表
    @GetMapping("/years")
    public ResponseEntity<List<Integer>> getAvailableYears() {
        List<Integer> years = holdingBatchService.getAvailableYears();
        return ResponseEntity.ok(years);
    }

    //检查指定年份数据是否存在
    @GetMapping("/check/{year}")
    public ResponseEntity<Map<String, Object>> checkYearData(@PathVariable int year) {
        boolean exists = holdingBatchService.checkYearDataExists(year);
        
        Map<String, Object> result = new HashMap<>();
        result.put("year", year);
        result.put("dataExists", exists);
        
        return ResponseEntity.ok(result);
    }
}